#!/bin/bash

__FileName__=""
__Author__="SuoSuo"
__Time__="2023/06/14"
__Version__="V1.0"


#显示程序的版本号和更新日期
version () {
	printf "\033[34m
=============================================================
*   ${PROGRAM_NAME} ${PROGRAM_VERSION}
*   ${AUTHOR_NAME}          ${AUTHOR_NUMBER}
=============================================================
Release Note:
    >>V1.0 20230614

---------------------------------------------------------------
    \033[0m\n"
	exit 0
}

format_centre_print () {
:<<!
para 1: 要打印的字符串
para 2: 占位数
para 3:
para 4: 是否换行，默认否 0/1
!
    Ss=$1
    Le=$2
    Nn=$(( (Le-${#Ss}) / 2 ))
    [ $Nn -lt 0 ] && Nn=0
    pre=$Nn
    suf=$Nn
    fff=$3
    [[ -n "$3" && "$3" -ge 1 ]] && suf=$((suf - fff))
    format_str="%${pre}s%s%${suf}s"
    [[ "$4" -eq 1 ]] && format_str=${format_str}"\n"
    printf "\033[34m${format_str}\033[0m"  "" "${Ss}" ""
#STR="eeeesfsfssfseee"; printf 'ddd %11s%-11s dddd \n' `echo $STR | cut -c 1-$((${#STR}/2))` `echo $STR | cut -c $((${#STR}/2+1))-${#STR}`
}
title_print () {
    while read line
    do
        name=`echo $line | awk '{print $1}' | xargs`
        len=`echo $line | cut -d' ' -f2 | xargs`
        suff=`echo $line | cut -d' ' -f3 | xargs`
        format_centre_print $name $len $suff
    done <<< $1
}

fc_card_bus  () {
    lspci | egrep -i 'qlogic|emulex' -q
    [[ $? -ne 0 ]] && { printf "\033[31mThe Machine Does Not Have a FC-HBA Adapter!!\033[0m\n" ; return; }
    printf "\033[32m                          FCHBA Card Adapter     \033[0m\n"
    # FC 卡
#    printf '[root@SuoSuo ~]#\033[35m lspci | egrep -i qlogic|emulex\033[0m\n'
    lspci | egrep -i 'qlogic|emulex'
    # Formatted output Title
    read -r -d '' titles <<- EOF
Name   12
Bus    14
WWN    19
State  9
Speed  10
DriName 10
DriVer  16
FwVer   8
Type   26  4
EOF
    title_print "${titles}"
    if [ ${SHOW_VERBOSE} -ge 1 ];then
        read -r -d '' titles <<- EOF
Slot    6
Numa    4
VD      10
SubVD  12
EOF
        title_print "${titles}"
    fi
#Support_Speed 26
    printf "\n"

    lspci | egrep -i 'qlogic|emulex' | awk '{print "0000:"$1}' | while read BDF
    do
        hostname=$(find /sys/bus/pci/devices/${BDF}/ -maxdepth 1 -type d -name 'host[0-9]*' | awk -F'/' '{print $NF}' | xargs)
        [[ -z "${hostname}" ]] && continue
        is_emulex=0
        lspci -s $BDF | grep -i 'Emulex' -q
        [[ $? -eq 0 ]] && is_emulex=1

        fc_home_dir="/sys/class/fc_host/${hostname}"
        cd $fc_home_dir
        # 产品名称
        product_name=$(lspci -vvv -s ${BDF} 2> /dev/zero | egrep '^[[:space:]]*Subsystem' | awk '{print $4}' | xargs)
        printf "%-13s" $product_name
        # BDF 号
        printf "%-14s" $BDF
        # wwn 号
        port_wwn=$(cat port_name | awk -F '0x' '{print $2}' | xargs)
        printf "%-18s" $port_wwn
        # 端口状态
        port_state=$(cat port_state)
        printf "%-9s" $port_state
        # 端口速率
        port_speed=$(cat speed | xargs | sed 's/ //g')
        printf "%-8s" $port_speed
        # 驱动名
        driver_name=$(lspci -vvv -s ${BDF} 2> /dev/zero | grep 'Kernel modules' | awk '{print $NF}')
        printf "%-9s" $driver_name
        # 驱动版本
        driver_version=$(modinfo ${driver_name} | egrep '^version' | awk '{print $NF}')
        if [[ $is_emulex -eq 1 ]]; then
            driver_version=$(echo $driver_version | sed 's/^0://g')
        fi
        printf "%-16s" $driver_version
        # FW 版本
        if [[ ${is_emulex} -ne 1 ]]; then
            fw_version=$(cat device/scsi_host/${hostname}/fw_version 2> /dev/null | awk '{print $1}' | xargs)
        else
            pci_home_dir="/sys/bus/pci/devices/${BDF}"
            cd $pci_home_dir
            fw_version=$(cat ${hostname}/scsi_host/${hostname}/fwrev 2> /dev/null | awk '{print $1}' | sed 's/,//g'|xargs )
        fi
        cd $fc_home_dir
        printf "%-16s" $fw_version
        # 端口类型
        port_type=$(cat port_type | awk '{print $1}' | xargs)
        printf "%-15s" $port_type
        if [ $? -ne 0 ]; then
            printf "\n"
            continue
        fi
        if [ ${SHOW_VERBOSE} -ge 1 ];then
            ids_str=`lspci -nvmms ${BDF} 2> /dev/null`
            # 槽位号
            slots=`echo ${ids_str} | awk -F 'PhySlot' '{print $2}' | awk '{print $2}' | xargs`
            printf "%-5s" "${slots}"
            # NUMA
            numanode=`echo ${ids_str} | awk -F 'NUMANode' '{print $2}' | awk '{print $2}' | xargs`
            printf "%-4s" "${numanode}"
            # 4 个 ID
            vendor=`echo ${ids_str} | awk -F 'Vendor' '{print $2}' | awk '{print $2}' | xargs`
            device=`echo ${ids_str} | awk -F 'Device' '{print $2}' | awk '{print $2}' | xargs`
            printf "%-10s" "${vendor}:${device}"
            svendor=`echo ${ids_str} | awk -F 'SVendor' '{print $2}' | awk '{print $2}' | xargs`
            sdevice=`echo ${ids_str} | awk -F 'SDevice' '{print $2}' | awk '{print $2}' | xargs`
            printf "%-10s" "${svendor}:${sdevice}"
        fi
        printf "\n"
        if [ ${SHOW_VERBOSE} -ge 2 ];then
            #端口支持速率
            printf "\033[33m%17s\033[0m" "Support_Speed:"
            port_supp_speeds=$(cat supported_speeds | xargs | sed 's/ //g')
            printf "%-28s\n" " ${port_supp_speeds}"
        fi
        if [ ${SHOW_VERBOSE} -ge 3 ];then
            lspci_str=`lspci -vv -s ${BDF} 2> /dev/null  | xargs -i echo "{}===" | xargs`
            printf "\033[33m%17s\033[0m" "LnkCap:"
            pci_LnkCap=`echo ${lspci_str} | awk -F 'LnkCap:' '{print $2}' | awk -F'===' '{print $1}' | awk -F',' '{ii=1;while(ii<=NF){if($ii ~ "Speed|Width")printf("%-16s",$ii);ii++}print " ";}' | xargs`
            printf "%-40s\n" " ${pci_LnkCap}"
            printf "\033[33m%17s\033[0m" "LnkSta:"
            pci_LnkSta=`echo ${lspci_str} | awk -F 'LnkSta:' '{print $2}' | awk -F'===' '{print $1}' | awk -F',' '{ii=1;while(ii<=NF){if($ii ~ "Speed|Width")printf("%-16s",$ii);ii++}print " ";}' | xargs`
            printf "%-40s\n" " ${pci_LnkSta}"
        fi
        wait
        wait
    done 2> /dev/zero

    #/sys/class/fc_host/host24/device/scsi_host/host24/fw_version
    #/sys/class/scsi_host/host*/fwrev
}


#当前时间，格式是“年.月.日.时.分.秒”
date_time=`date +%Y.%m.%d.%H.%M.%S`
#文件名称
PROGRAM_NAME=$(basename $0)
#作者名字
AUTHOR_NAME="SuoSuo"
#作者工号
AUTHOR_NUMBER="50023"
# 版本
PROGRAM_VERSION="V1.0"
# 首地址
BASE_DIR=`pwd`
SHOW_VERBOSE=0

#显示帮助目录菜单
usage_and_exit () {
    echo
    printf "${PROGRAM_NAME} Version ${PROGRAM_VERSION}\n"
	printf "\\033[32m
    FC Adapter Information Profile
Usage:
  $PROGRAM_NAME [options]
Options:
    -v           Be Verbose
    -h           Show Help Message
    -b           Show Version
    \n\
Attention:
    1>.
    2>.
Examples:
    ./${PROGRAM_NAME} -v
    ./${PROGRAM_NAME} -vvv
Questions:
    1>.
    2>.   \
    \033[0m\n
"
    exit 1
}

while getopts "vhb" arg
do
	case $arg in
	    v)
	    let SHOW_VERBOSE++ ;;
		h)
		usage_and_exit ;;
		b)
		version ;;
	esac
done

main () {
    fc_card_bus
}

main